﻿using Nop.Core.Configuration;
using Nop.Web.Services.Mob;
using PayCenter.Sdk;
using System;
using System.Collections.Generic;
using System.Text;

namespace Nop.Web.Services.Utils
{
    public class HPaySdkHelper
    {
        static object lockObj = new object();
        static Dictionary<string, PaySdk> sdkList = new Dictionary<string, PaySdk>();
        private readonly NopConfig _config;
   

        public HPaySdkHelper(NopConfig config )
        {
            this._config = config;
          
        }

        public PaySdk Get()
        {
            return Get(this._config.HPayConnectionString);

        }

        public PaySdk Get(string connectionString)
        {
            lock (sdkList)
            {
                if (!sdkList.ContainsKey(connectionString))
                { 
                    sdkList[connectionString] = CreateSdk(connectionString);
                }
                return sdkList[connectionString];
            }
        }

        private PaySdk CreateSdk(string connectionString)
        {
            //signKey@address:port
            if (connectionString.IndexOf("@") > -1 && connectionString.IndexOf(":") > -1 && connectionString.IndexOf(":") > -1)
            {
                string merchant = connectionString.Split('@')[0];
                var merchantId = long.Parse(merchant.Split(':')[0]);
                string signKey = merchant.Split(':')[1];
                string server = connectionString.Split('@')[1];
                string address = server.Split(':')[0];
                var port = int.Parse(server.Split(':')[1]);
                var sdk = new PaySdk(merchantId, signKey, address, port); 
                return sdk;
            }
            else
            {

                throw new NotSupportedException("sdk连接字符串错误");
            }
        }

      
    }


}
